﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client;

namespace SomethingSpacial.Views.MixItUp
{
    public partial class MapControl : UserControl
    {
        private string _state { get; set; }
        private string _city { get; set; }
        private string _zip { get; set; }
        private string _street { get; set; }

        private bool bMapLoaded = false;
        private bool bAddressToZoom = false;

        public MapControl()
        {
            InitializeComponent();
        }

        private void MyMap_Loaded(object sender, RoutedEventArgs e)
        {
            if (bAddressToZoom == true)
                Zoom();
        }

        public void ZoomToAddress(string Street, string City, string sZip, string State)
        {
            _street = Street;
            _city = City;
            _state = State;
            _zip = _zip;

            bAddressToZoom = true;

            if (bMapLoaded == true)
                Zoom();
        }

        private void Zoom()
        {
            Locator locatorTask = new Locator("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
                "Locators/ESRI_Geocode_USA/GeocodeServer");
            
            locatorTask.AddressToLocationsCompleted += new EventHandler<AddressToLocationsEventArgs>(locatorTask_AddressToLocationsCompleted);
            AddressToLocationsParameters addressParams = new AddressToLocationsParameters();
            Dictionary<string, string> address = addressParams.Address;
                        
            address.Add("Address", _street);
            address.Add("City", _city);
            address.Add("State", _state);
            address.Add("Zip", _zip);

            locatorTask.AddressToLocationsAsync(addressParams);
        }

        void locatorTask_AddressToLocationsCompleted(object sender, AddressToLocationsEventArgs e)
        {
             List<AddressCandidate> returnedCandidates = e.Results;

            GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();

            if (returnedCandidates.Count > 0)
            {
                AddressCandidate candidate = returnedCandidates[0];

                Graphic graphic = new Graphic()
                {
                    Symbol = strobeSymbol,
                    Geometry = candidate.Location
                };

                graphic.Attributes.Add("Address", candidate.Address);

                string latlon = String.Format("{0}, {1}", candidate.Location.X, candidate.Location.Y);
                graphic.Attributes.Add("LatLon", latlon);

                graphicsLayer.Graphics.Add(graphic);
               
                MyMap.PanTo(candidate.Location);
                MyMap.ZoomToResolution(0.000332333, candidate.Location);
            }
        }
    }
}
